{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "DkA0Fobtb9dM"
   },
   "source": [
    "##### Copyright 2022 The Cirq Developers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "id": "tUshu7YfcAAW"
   },
   "outputs": [],
   "source": [
    "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "_JFVRPQ1l17m"
   },
   "source": [
    "# QVM Creation Template"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "E6JaKuNTl9SA"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/simulate/qvm_builder_code\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/main/docs/simulate/qvm_builder_code.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/main/docs/simulate/qvm_builder_code.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/simulate/qvm_builder_code.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8e6bafdbda4d"
   },
   "source": [
    "This notebook includes a couple of clean and succinct code blocks that you can build on or copy and paste elsewhere in order to make use of the [Quantum Virtual Machine](./quantum_virtual_machine.ipynb) without worrying about how it works inside. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Lfira0gPf0Gd"
   },
   "source": [
    "## **Install** Cirq and qsim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "id": "zs5J6wAXqvtW"
   },
   "outputs": [],
   "source": [
    "# @title Install `cirq_google` and `qsimcirq`\n",
    "\n",
    "try:\n",
    "    import cirq\n",
    "    import cirq_google\n",
    "except ImportError:\n",
    "    print(\"installing cirq...\")\n",
    "    !pip install --quiet cirq-google\n",
    "    print(\"installed cirq.\")\n",
    "    import cirq\n",
    "    import cirq_google\n",
    "\n",
    "try:\n",
    "    import qsimcirq\n",
    "except ImportError:\n",
    "    print(\"installing qsimcirq...\")\n",
    "    !pip install --quiet qsimcirq\n",
    "    print(f\"installed qsimcirq.\")\n",
    "    import qsimcirq\n",
    "\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "p2JAfQa8gVSe"
   },
   "source": [
    "## Create a **Quantum Virtual Machine**.\n",
    "\n",
    "Instantiate a `cirq.SimulatedLocalEngine` that uses the [Virtual Engine Interface](./virtual_engine_interface.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "id": "pbHCUPLpq5WE"
   },
   "outputs": [],
   "source": [
    "# @title Choose a processor (\"willow_pink\" or \"rainbow\" or \"weber\")\n",
    "# (see cirq_google.engine.list_virtual_processors() for available names)\n",
    "processor_id = \"willow_pink\"  # @param {type:\"string\"}\n",
    "\n",
    "# Instantiate an engine.\n",
    "sim_engine = cirq_google.engine.create_default_noisy_quantum_virtual_machine(\n",
    "    processor_id=processor_id, simulator_class=qsimcirq.QSimSimulator\n",
    ")\n",
    "print(\n",
    "    \"Your quantum virtual machine\",\n",
    "    processor_id,\n",
    "    \"is ready, here is the qubit grid:\",\n",
    "    \"\\n========================\\n\",\n",
    ")\n",
    "print(sim_engine.get_processor(processor_id).get_device())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TJfN17frwo-0"
   },
   "source": [
    "## **Create** a device-ready circuit."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BYxi9xpXjJdI"
   },
   "source": [
    "To learn how to create a device ready circuit, have a look at the [QVM Circuit Preparation](./qvm_basic_example.ipynb) page."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "FCoKJhGri8lR"
   },
   "outputs": [],
   "source": [
    "# create your device ready circuit here!\n",
    "q0 = cirq.GridQubit(6, 3)\n",
    "your_circuit = cirq.Circuit([(cirq.X**0.5)(q0), cirq.measure(q0)])\n",
    "print(your_circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Zxv0RtJuhaof"
   },
   "source": [
    "## **Execute** Your circuit on the Quantum Virtual Machine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "id": "bFjnNSqRZsFu"
   },
   "outputs": [],
   "source": [
    "# @title Enter the name of your device ready circuit and execute it on the Quantum Virtual Machine\n",
    "circuit = your_circuit  # @param\n",
    "\n",
    "reps = 3000\n",
    "start = time.time()\n",
    "results = sim_engine.get_sampler(processor_id).run(circuit, repetitions=reps)\n",
    "elapsed = time.time() - start\n",
    "\n",
    "print('Circuit successfully executed on your quantum virtual machine', processor_id)\n",
    "print(f'QVM runtime: {elapsed:.04g}s ({reps} reps)')\n",
    "print('You can now print or plot \"results\"')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "98bf142f5be6"
   },
   "outputs": [],
   "source": [
    "print(f\"measurements: {results.histogram(key=q0)}\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "qvm_builder_code.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
